﻿

using System;
using System.Data;
using Model;
namespace Dal
{
	public class DbBannerDal
	{
		public DbBannerDal()
		{
		}
 		#region Insert
 		public static int Insert(DbBanner dbBanner)
		{	
			string sql = @"Insert Into DbBanner (
					BannerName,
					BannerUrl,
					Status,
					OrderNum,
					AddTime,
					LinkUrl,
					IsWhere
				) values('{0}','{1}',{2},{3},'{4}','{5}',{6})";
			sql = string.Format(sql,
					SqlHelper.String(dbBanner.BannerName),
					SqlHelper.String(dbBanner.BannerUrl),
					dbBanner.Status,
					dbBanner.OrderNum,
					dbBanner.AddTime,
					SqlHelper.String(dbBanner.LinkUrl),
					dbBanner.IsWhere
				);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
		
		#region Update
 		public static int Update(DbBanner dbBanner)
		{	
			string sql = @"Update DbBanner set 

					BannerName= '{0}',
					BannerUrl= '{1}',
					Status= {2},
					OrderNum= {3},
					AddTime= '{4}',
					LinkUrl= '{5}',
					IsWhere= {6}
				where

					BannerID= {7}";
			sql = string.Format(sql,
					SqlHelper.String(dbBanner.BannerName),
					SqlHelper.String(dbBanner.BannerUrl),
					dbBanner.Status,
					dbBanner.OrderNum,
					dbBanner.AddTime,
					SqlHelper.String(dbBanner.LinkUrl),
					dbBanner.IsWhere,
					dbBanner.BannerID);
			return SqlHelper.ExecSql(sql);
		}
		#endregion
 
 

 		#region Delete
 		public static int Delete(Int32 bannerID)
		{
		
			string sql = @"Delete from DbBanner where 
					BannerID= {0}";
			sql = string.Format(sql,
					bannerID
);
			return SqlHelper.ExecSql(sql);
		} 
		#endregion
 
 		#region Select
 		
 		public static DataSet Select()
		{
			string sql = "select a.* from DbBanner a "; 
			return SqlHelper.Select(sql);
		}
				
		public static DbBanner Select(Int32 bannerID)
		{
			DbBanner dbBanner = null;
			string sql = @"select a.* from DbBanner a  where
					a.BannerID= {0}
";
			sql = string.Format(sql,
					bannerID
);
			DataSet ds = SqlHelper.Select(sql);
			if(SqlHelper.HasRow(ds))
				dbBanner = new DbBanner(ds.Tables[0].Rows[0]);					
			return dbBanner;
		}
		
		public static DataSet Select(string where)
		{ 
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from DbBanner a  where 1=1 " + where ;
			return SqlHelper.Select(sql);
		}
		
		public static DataSet SelectForList(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;
						
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;
			string sql = "select a.* from dbBanner a where 1=1 " + where;
                
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}		
		public static DataSet Select(string where,int pageIndex,int pageSize,bool bCount,out int count )
		{
			count = 0;			
			if (where.Trim().ToLower().StartsWith("where"))
				where = where.Substring(5);
			if (!where.Trim().ToLower().StartsWith("and") && where.Trim().Length > 0)
				where = " and " + where;			
			string sql = "select a.* from DbBanner a  where 1=1 " + where;
			return SqlHelper.Select(sql,pageSize,pageIndex,bCount,ref count);
		}
		#endregion
	}
}
